﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CrmManageSystemClient.Formn.Freight
{
    public partial class FmFreight_Main : Form
    {
        public FmFreight_Main()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 开始行索引Id
        /// </summary>
        int _startRowIndexId = 0;
        /// <summary>
        /// 当前页码
        /// </summary>
        int _NowPageRows = 1;
        /// <summary>
        /// 最大数量的行数量
        /// </summary>
        int _maxNumberRows = 0;

        int _TotalPages = 0;

        private void Freight_Load(object sender, EventArgs e)
        {
            //加载快递

            //设置行数量
            _maxNumberRows = System.Convert.ToInt32(textBox1.Text);

            listViewAdd();
        }





        #region 插入listView
        /// <summary>
        /// 插入listView
        /// </summary>
        private void listViewAdd()
        {
            try
            {
                int Total = 0;
                //获取分页列表
                IList<Logistics.Model.Model_Xd5u_Express_Info> Express_Infolist =
                    Express_Infolist = Logistics.Helper.CommonHelper.GetExpress_InfoList(_startRowIndexId, _maxNumberRows, out Total);

                //总页数
                _TotalPages = (Total + _maxNumberRows - 1) / _maxNumberRows;
                label2.Text = _TotalPages.ToString();
                comboBox1.Items.Clear();
                for (int i = 0; i < (Total + _maxNumberRows - 1) / _maxNumberRows; i++)
                {
                    comboBox1.Items.Add(i + 1);
                }

                label4.Text = _NowPageRows.ToString();
                label5.Text = Total.ToString();

                listView1.Items.Clear();
                foreach (Logistics.Model.Model_Xd5u_Express_Info stu in Express_Infolist)
                {
                    ListViewItem ListViewGroup = new ListViewItem();
                    ListViewGroup.Text = stu.Id.ToString();
                    ListViewGroup.SubItems.Add(stu.OrderNumber);
                    ListViewGroup.SubItems.Add(stu.Consignee);
                    ListViewGroup.SubItems.Add(stu.Sender);
                    ListViewGroup.SubItems.Add(stu.CategoryId.ToString());
                    ListViewGroup.SubItems.Add(stu.CreateTime.ToString());
                    //ListViewGroup.SubItems.Add(stu.UpdateTime.ToString());
                    //ListViewGroup.SubItems.Add(stu.OperateUserName);
                    ListViewGroup.SubItems.Add(stu.Note);
                    listView1.Items.Add(ListViewGroup);
                }

                if (_NowPageRows > 1)
                {
                    button3.Enabled = true;
                }
                else
                {
                    button3.Enabled = false;
                }
                if (_NowPageRows < _TotalPages)
                {
                    button4.Enabled = true;
                }
                else
                {
                    button4.Enabled = false;
                }
            }
            catch { }

        }
        #endregion

        private void button3_Click(object sender, EventArgs e)
        {
            _startRowIndexId = _startRowIndexId - System.Convert.ToInt32(textBox1.Text);

            //if (_NowPageRows>=0)
            _NowPageRows--;
            _maxNumberRows = System.Convert.ToInt32(textBox1.Text);

            listViewAdd();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            _startRowIndexId = _startRowIndexId + System.Convert.ToInt32(textBox1.Text);

            _NowPageRows++;
            _maxNumberRows = System.Convert.ToInt32(textBox1.Text);

            listViewAdd();
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            _startRowIndexId = System.Convert.ToInt32(textBox1.Text) * (System.Convert.ToInt32(comboBox1.SelectedItem) - 1);

            //if (_NowPageRows>=0)
            _NowPageRows = System.Convert.ToInt32(comboBox1.SelectedItem);
            _maxNumberRows = System.Convert.ToInt32(textBox1.Text);

            listViewAdd();
        }


        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ListViewHitTestInfo info = listView1.HitTest(e.X, e.Y);
            if (info.Item != null)
            {
                Logistics.Model.Model_Xd5u_Express_Info _Model_Xd5u_Express_Info =
                    new Logistics.Model.Model_Xd5u_Express_Info();
                _Model_Xd5u_Express_Info.Id = Guid.Parse( info.Item.Text);
                _Model_Xd5u_Express_Info.OrderNumber = info.Item.SubItems[1].Text;
                _Model_Xd5u_Express_Info.Consignee = info.Item.SubItems[2].Text;
                _Model_Xd5u_Express_Info.Sender = info.Item.SubItems[3].Text;
                _Model_Xd5u_Express_Info.CategoryId = Guid.Parse(info.Item.SubItems[4].Text);
                _Model_Xd5u_Express_Info.CreateTime = DateTime.Parse(info.Item.SubItems[5].Text);
                _Model_Xd5u_Express_Info.Note = info.Item.SubItems[6].Text;

                Freight.FmFreight_Details _Freight_Details = new FmFreight_Details(_Model_Xd5u_Express_Info);
                _Freight_Details.ShowDialog();

                //MessageBox.Show(_Model_Xd5u_Express_Info.Id + System.Environment.NewLine + _Model_Xd5u_Express_Info.OrderNumber + System.Environment.NewLine + _Model_Xd5u_Express_Info.Consignee);
            }
        }
    }
}
